package com.linln.modules.message.service.impl;


import com.linln.modules.message.domain.MessageRefund;
import com.linln.modules.message.repository.MessageRefundRepository;
import com.linln.modules.message.service.MessageRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageRefundServiceImpl implements MessageRefundService {

    @Autowired
    private  MessageRefundRepository messageRefundRepository;

    @Override
    public Integer readMessage(Long refundId) {
        if(refundId == null || refundId.longValue() == 0l){
            return 0;
        }
        Integer integer = messageRefundRepository.readMessage(refundId);
        return integer;
    }

    @Override
    public List<MessageRefund> selectUnreadRefund(Long memeberId) {
        if(memeberId == null || memeberId.longValue() == 0l){
            return null;
        }
        return messageRefundRepository.selectUnreadRefund(memeberId);
    }

    @Override
    public List<MessageRefund> selectAllRefund(Long memeberId) {
        if(memeberId == null || memeberId.longValue() == 0l){
            return null;
        }
        List<MessageRefund> messageRefunds = messageRefundRepository.selectAllRefundNoRead(memeberId);
        List<MessageRefund> messageRefunds1 = messageRefundRepository.selectAllRefundRead(memeberId);
        List<MessageRefund> result = new ArrayList<>();
        result.addAll(messageRefunds);
        Integer index=0;
        if(messageRefunds != null &&  messageRefunds.size() > 0){
            index =messageRefunds.size();
        }
        result.addAll(index,messageRefunds1);
        return result;
    }



    @Override
    public List<MessageRefund> selectAllRefund(Long memeberId,Integer type) {
        if(memeberId == null || memeberId.longValue() == 0l){
            return null;
        }
        List<MessageRefund> messageRefunds = messageRefundRepository.selectAllRefundNoRead(memeberId,type);
        List<MessageRefund> messageRefunds1 = messageRefundRepository.selectAllRefundRead(memeberId,type);
        List<MessageRefund> result = new ArrayList<>();
        result.addAll(messageRefunds);
        Integer index=0;
        if(messageRefunds != null &&  messageRefunds.size() > 0){
            index =messageRefunds.size();
        }
        result.addAll(index,messageRefunds1);
        return result;
    }

    @Override
    public Integer saveMessageRefund(MessageRefund messageRefund) {
        Integer row = messageRefundRepository.saveRefund(messageRefund.getMessageId(),messageRefund.getMemberId(),messageRefund.getAdminId(),messageRefund.getContent(),messageRefund.getStatus(),messageRefund.getType());
        return row;
    }


    @Override
    public Integer hasNoRead(Long memeberId) {
        List<MessageRefund> messageRefunds = messageRefundRepository.selectAllRefundNoRead(memeberId);
        if(messageRefunds != null && messageRefunds.size() > 0){
                return messageRefunds.size();
        }
        return 0;
    }
}
